package com.atguigu.vod.controller;

import com.atguigu.utils.R;
import com.atguigu.vod.service.VodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author zy
 */
@RestController
@CrossOrigin
@Api(value = "vodController", description = "视频文件处理-8003端口")
@RequestMapping("/eduvod/service")
@Slf4j
public class VodController {

    @Autowired
    VodService vodService;

    @ApiOperation("视频文件上传接口")
    @PostMapping("/uploadVideo")
    public R uploadVideo(MultipartFile file) {
        log.error("" + file.getOriginalFilename());
        try {
            String videoId = vodService.uploadVideo(file);
            return R.ok().data("videoId", videoId);
        } catch (IOException e) {
            e.printStackTrace();
            return R.error().data("videoId", null);
        }
    }

    @ApiOperation("视频文件删除接口")
    @DeleteMapping("/delVideo/{id}")
    public R delVideo(@PathVariable String id) {
        if (StringUtils.isEmpty(id)) {
            return R.error().message("参数不可为空!");
        }
        boolean b = vodService.removeAlyVideo(id);
        if (b) {
            return R.ok();
        }
        return R.error();
    }

    @ApiOperation("视频文件删除接口(多个 /ids)")
    @DeleteMapping("/delVideoByList")
    public R delVideoByList(@RequestParam List<String> ids) {
        if (ids != null && ids.size() > 0) {
            vodService.removeListAlyVideo(ids);
            return R.ok();
        } else {
            return R.error().message("参数不能为空");
        }
    }

    /**
     * 获取视频播放凭证
     *
     * @param videoSourceId videoSourceId
     * @return R
     */
    @ApiOperation("视频文件播放接口()")
    @GetMapping("/getVideoPlayOauth/{videoSourceId}")
    public R getPlayOauth(@PathVariable String videoSourceId) {
        String playAuth = vodService.getVideoPalyOauth(videoSourceId);
        return R.ok().data("playOauth", playAuth);
    }
}
